package m.jclecai.com.base;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import m.jclecai.com.R;

/**
 * 相同的部分由父类去实现
 * 不同的部分制定规则
 */
public abstract class OldBaseFragment extends OldSuperBaseFragment {
    private TextView tv_mid_title;
    private TextView tv_right_title;
    private LinearLayout ll_no_data;
    private LinearLayout ll_no_network;
    private RelativeLayout include_no_data;
    private RelativeLayout include_no_network;
    private ImageView iv_back;
    private FrameLayout fg_view;
    private View view;

    public OldBaseFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.base_fragment_title, container, false);
        initView(view, inflater, container, savedInstanceState);

        //通知子类去请求数据
        viewCreated(view, savedInstanceState);
        return view;
    }

    /**
     * 初始化
     */
    private void initView(View view, LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {
        tv_mid_title = (TextView) view.findViewById(R.id.tv_mid_title);
        tv_right_title = (TextView) view.findViewById(R.id.tv_right_title);
        ll_no_data = (LinearLayout) view.findViewById(R.id.ll_no_data);
        ll_no_network = ((LinearLayout) view.findViewById(R.id.ll_no_network));
        tv_right_title.setVisibility(View.GONE);

        include_no_data = (RelativeLayout) view.findViewById(R.id.include_no_data);
        include_no_network = (RelativeLayout) view.findViewById(R.id.include_no_network);
        iv_back = (ImageView) view.findViewById(R.id.iv_back);
        fg_view = (FrameLayout) view.findViewById(R.id.fg_view);
        fg_view.addView(createView(inflater, container, savedInstanceState));
    }

    /**
     * 具体的布局由子类去实现，父类只需要将这个布局添加进来即可
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    public abstract View createView(LayoutInflater inflater, ViewGroup container,
                                    Bundle savedInstanceState);

    /**
     * 此方法标志着父类的初始化已经完成，子类即可在此方法中做数据请求等
     *
     * @param view
     * @param savedInstanceState
     */
    public abstract void viewCreated(View view, Bundle savedInstanceState);

    /**
     * 设置title
     *
     * @param str
     */
    protected void setTitle(String str) {
        tv_mid_title.setText(str);
    }

    /**
     * 设置右边的title
     *
     * @param str
     */
    protected void setTitleRight(String str) {
        tv_right_title.setText(str);
    }

    /**
     * 显示没有数据时的图片
     */
    protected void setNoDataShow(boolean b) {
        if (b) {
            ll_no_data.setVisibility(View.VISIBLE);
        } else {
            ll_no_data.setVisibility(View.GONE);
        }
    }

    /**
     * 显示没有网络的图片
     */
    protected void setNoNewWork(boolean b) {
        if (b) {
            ll_no_network.setVisibility(View.VISIBLE);
        } else {
            ll_no_network.setVisibility(View.GONE);
        }
    }

    /**
     * 显示或隐藏返回键
     *
     * @param b
     */
    protected void setBackShow(boolean b) {
        if (b) {
            iv_back.setVisibility(View.INVISIBLE);
        } else {
            iv_back.setVisibility(View.GONE);
        }
    }
    /**
     * 通过类名启动activity
     *
     * @param pClass
     *            要启动的类
     * @param pBundle
     *            要传递的参数
     */
    protected void openActivity(Class<?> pClass, Bundle bundle) {
        Intent intent = new Intent(getActivity(), pClass);
        if (bundle != null) {
            intent.putExtra("bundle", bundle);
        }
        startActivity(intent);
    }
}
